package client;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ChatClientView extends JFrame implements ChatClientGUI{
        private JPanel toppanel,middlepanel,luecke,luecke2;
        private JLabel name,ip,port,chat,user,nachricht;
        private JTextField nachrichtfield,ipfield,portfield,namefield;
        private JTextArea chatarea,userarea;
        private JButton connect,disconnect,send;
        private ChatClientPresenter presenter;
        
        public ChatClientView(){
                super("Ein einfacher Chatclient");
                initView();
                ChatClientGUI g = this;
                presenter = new ChatClientPresenter(g);
        }

        private void initView() {
                this.setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
                toppanel = new JPanel();
                middlepanel = new JPanel();
                toppanel.setLayout(new GridLayout(2,3));
                middlepanel.setLayout(new GridBagLayout());
                this.add(toppanel);
                this.add(middlepanel);
                name = new JLabel("Name:");
                ip = new JLabel("IP:");
                port = new JLabel("Port:");
                toppanel.add(name);
                toppanel.add(ip);
                toppanel.add(port);
                ipfield = new JTextField(15);
                namefield = new JTextField(15);
                portfield = new JTextField(15);
                namefield.setColumns(15);
                toppanel.add(namefield);
                toppanel.add(ipfield);
                toppanel.add(portfield);
                chat = new JLabel("Chat:");
                user = new JLabel("Benutzer:");
                luecke = new JPanel();
                nachricht = new JLabel("Nachricht:");
                nachrichtfield = new JTextField(30);
                send = new JButton("Nachricht senden");
                connect = new JButton("Mit Chatserver verbinden");
                disconnect = new JButton("Chatserver verlassen");
                luecke2 = new JPanel();
                GridBagConstraints c = new GridBagConstraints();
                c.gridx = 0;
                c.gridy = 0;
                c.anchor = GridBagConstraints.FIRST_LINE_START;
                middlepanel.add(chat,c);
                c.gridx = 2;
                middlepanel.add(user,c);
                c.gridx = 0;
                c.gridy = 1;
                middlepanel.add(new JScrollPane(chatarea = new JTextArea(15,30)),c);
                chatarea.setLineWrap(true);
                chatarea.setEditable(false);
                c.gridx = 1;
                middlepanel.add(luecke,c);
                c.gridx = 2;
                middlepanel.add(new JScrollPane(userarea = new JTextArea(15,15)),c);
                userarea.setEditable(false);
                c.gridy = 2;
                c.gridx = 0;
                middlepanel.add(nachricht,c);
                c.gridy = 3;
                middlepanel.add(nachrichtfield,c);
                c.gridx = 2;
                middlepanel.add(send,c);
                c.gridy = 4;
                c.gridx = 0;
                middlepanel.add(luecke2,c);
                c.gridy = 5;
                middlepanel.add(connect,c);
                c.gridx = 2;
                middlepanel.add(disconnect,c);
                
                this.pack();
                this.setResizable(false);
                
                connect.addActionListener(new ActionListener(){
                	public void actionPerformed(ActionEvent arg0){
                		presenter.tryConnection(ipfield.getText(), Integer.parseInt(portfield.getText()), namefield.getText());
                	}
                });
                
                send.addActionListener(new ActionListener(){
                	public void actionPerformed(ActionEvent arg0){
                		presenter.sendMessage(nachrichtfield.getText());
                	}
                });
                
                disconnect.addActionListener(new ActionListener(){
                	public void actionPerformed(ActionEvent arg0){
                		presenter.disconnect();
                	}
                });
        }
        
        public void updateChat(String s){
        	chatarea.append(s+"\n");
        }
        public void updateUserList(String s){
        	userarea.setText(s);
        }
}
